package cn.team11.sds.dayi.debugpage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import cn.team11.sds.dayi.R;

import com.aliyun.alink.business.downstream.DeviceBusiness;
import com.aliyun.alink.business.login.AlinkLoginBusiness;
import com.aliyun.alink.page.pageroutor.ARouter;
import com.aliyun.alink.page.pageroutor.listener.IPanelListener;
import com.aliyun.alink.page.pageroutor.listener.IRouterListener;
import com.aliyun.alink.sdk.net.anet.api.persistentnet.IOnPushListener;
import com.android.volley.RequestQueue;

import cn.team11.sdsPackager.T11PackageConfig;
import cn.team11.sdsPackager.TestPackageConfig_test;
import cn.team11.smarteye.misc.SConstants;
import cn.team11.smarteye.utils.PreferencesUtils;
import cn.team11.util.T11Log;
import cn.team11.util.update.T11UpdateService;

public class WelcomeActivity extends Activity {

    private static final String TAG = "WelcomeActivity";

    private DeviceBusiness deviceBiz = null;

    private RequestQueue requestQueue = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        T11UpdateService.startVersionCheck(this);
        initPushTunnel();
        if (!isConnectingToInternet()) {
            t11InitView();
        } else {
            PreferencesUtils.putString(SConstants.USERNAME, getUserID());
            loadBone(null);
        }
    }


    /**
     * 获取SDS用户账号
     * @return
     */
    public String getUserID(){
        try {
            if(AlinkLoginBusiness.getInstance().isLogin()){
                String userID = AlinkLoginBusiness.getInstance().getUserID();
                return userID;
            }
        }catch (Exception e){
            T11Log.e(e.toString());
            return "";
        }
        return "";
    }

    public boolean isConnectingToInternet() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        //新版本调用方法获取网络状态
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Network[] networks = connectivityManager.getAllNetworks();
            NetworkInfo networkInfo;
            for (Network mNetwork : networks) {
                networkInfo = connectivityManager.getNetworkInfo(mNetwork);
                if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                    return true;
                }
            }
        } else {
            //否则调用旧版本方法
            if (connectivityManager != null) {
                NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
                if (info != null) {
                    for (NetworkInfo anInfo : info) {
                        if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                            T11Log.d("NETWORKNAME: " + anInfo.getTypeName());
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }

    private void t11InitView() {
        setContentView(R.layout.activity_welcome);
        ImageView img = (ImageView) findViewById(R.id.offLineImg);
        int imgWidth = img.getWidth();
        img.setMaxHeight(imgWidth);
        img.setMinimumHeight(imgWidth);
    }


    private void initPushTunnel() {
        deviceBiz = new DeviceBusiness();
        deviceBiz.startWatching(DeviceBusiness.FEATURE_DOWNSTREAM_WATCHER);
        deviceBiz.setDownstreamListener(new IOnPushListener() {
            @Override
            public void onCommand(String s) {
                Toast.makeText(WelcomeActivity.this, "收到下推数据", Toast.LENGTH_SHORT).show();
                T11Log.d("收到下推数据：" + s);
            }

            @Override
            public boolean filter(String s) {
                return true;
            }
        }, true);
    }

//    @Override
//    protected void onStop() {
//        super.onStop();
//        T11Log.d("onStop");
//        t11InitView();
//    }

    public void loadBone(View view) {

        String boneUrl = "";
        //模拟测试
//        boneUrl = TestPackageConfig_test.boueUrl;
        //正式
        boneUrl = T11PackageConfig.boneUrl;
        T11Log.d("boneUrl = " + boneUrl);
        ARouter.navigate(WelcomeActivity.this, boneUrl);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (null != requestQueue) {
            requestQueue.stop();
        }
    }
}
